﻿Imports ICSharpCode.SharpZipLib
Imports System.IO.IsolatedStorage
Imports System.IO
Imports ICSharpCode.SharpZipLib.Zip
Imports System.Text
Imports Microsoft.Phone.Tasks

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    Private fileStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    Private settings As IsolatedStorage.IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings

    ' Constructor
    Public Sub New()
        InitializeComponent()

        Dim sb As New StringBuilder
        With sb
            .AppendLine("Application Name: Galgame Player")
            .AppendLine("Version:" & My.Application.Info.Version.ToString())
            .AppendLine("Author: LonghronShen@wpbeta.org")
            TB_About.Text = .ToString()
        End With

#If Not DEBUG Then
        If fileStorage.FileExists("Sample.zip") = False Then
#Else
        If fileStorage.DirectoryExists("Sample") Then
            fileStorage.DeleteDirectory("Sample", True)
        End If
#End If
        Using ms As New MemoryStream(My.Resources.Resource.Sample)
            Using isolatedFileStream = New IsolatedStorageFileStream("Sample.zip", FileMode.OpenOrCreate, fileStorage)
                Using fileWriter = New IO.BinaryWriter(isolatedFileStream)
                    fileWriter.Write(ms.ToArray())
                    fileWriter.Close()
                End Using
                isolatedFileStream.Close()
            End Using
            ms.Close()
        End Using
#If Not DEBUG Then
        End If
#End If

        g_gameList.Clear()
        Dim gameList = fileStorage.GetFileNames("*.zip")
        For Each game In gameList
            Try
                Dim gParser As New GalgameParser.GalgameParser(game)
                g_gameList.Add(gParser)
            Catch ex As Exception
                Continue For
            End Try
        Next

        Me.LB_List.ItemsSource = g_gameList
    End Sub

    Private Sub TextBlock_GameItem_Tap(sender As System.Object, e As System.Windows.Input.GestureEventArgs)
        Dim gameName = Uri.EscapeDataString(CType(CType(sender, TextBlock).Tag, GalgameParser.GalgameParser).Name)
        Dim destination As String = "/GamePage.xaml?GameName=" & gameName
        Me.NavigationService.Navigate(New Uri(destination, UriKind.Relative))
    End Sub

End Class